﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKRevitKernel.RevitObjectSerializeUtility;

namespace YJKRevitArea.AreaThumbnail
{
	public partial class FormFirePreventionThumbnailSetUp : System.Windows.Forms.Form
	{
		public FormFirePreventionThumbnailSetUp()
		{
			this.InitializeComponent();
			if (File.Exists(this.m_strFilePath))
			{
				Exception ex;
				new HYRevitObjectSerializeManger().TryGetAPersistentClassByXML<FirePreventionThumbnailSetUpData>(this.m_strFilePath, out this.m_FirePreventionThumbnailSetUpDataSave, out ex, true);
				if (this.m_FirePreventionThumbnailSetUpDataSave == null)
				{
					this.m_FirePreventionThumbnailSetUpDataSave = new FirePreventionThumbnailSetUpData();
				}
				this.txtCharacterHeight.Text = this.m_FirePreventionThumbnailSetUpDataSave.CharacterHeight.ToString();
				this.txtRatioOfHeightToWidth.Text = this.m_FirePreventionThumbnailSetUpDataSave.RatioOfHeightToWidth.ToString();
			}
			else
			{
				this.txtCharacterHeight.Text = "3.5";
				this.txtRatioOfHeightToWidth.Text = "0.7";
			}
			this.InitAllTypeface();
			this.Refresh();
		}

		public FirePreventionThumbnailSetUpData SetFirePreventionThumbnailSetUpData()
		{
			this.m_FirePreventionThumbnailSetUpData.TypefaceName = this.cmbTypeface.Text;
			this.m_FirePreventionThumbnailSetUpData.CharacterHeight = this.SetFirePreventionThumbnailSetUpData(this.txtCharacterHeight, 3.5);
			this.m_FirePreventionThumbnailSetUpData.RatioOfHeightToWidth = this.SetFirePreventionThumbnailSetUpData(this.txtRatioOfHeightToWidth, 0.7);
			return this.m_FirePreventionThumbnailSetUpData;
		}

		public double SetFirePreventionThumbnailSetUpData(System.Windows.Forms.TextBox text, double dDefaultNum)
		{
			if (text.Text.Equals("0") || text.Text.Equals("") || text.Text == string.Empty)
			{
				return dDefaultNum;
			}
			double result;
			try
			{
				result = Convert.ToDouble(text.Text);
			}
			catch
			{
				result = dDefaultNum;
			}
			return result;
		}

		public void JudgeFirePreventionThumbnailSetUpData(System.Windows.Forms.TextBox text, double dDefaultNum)
		{
			if (text.Text.Equals("0") || text.Text.Equals("") || text.Text == string.Empty)
			{
				text.Text = dDefaultNum.ToString();
				return;
			}
			try
			{
				Convert.ToDouble(text.Text);
			}
			catch
			{
				YJKMessageBox.Show("输入的值有误");
				text.Text = dDefaultNum.ToString();
			}
		}

		private void InitAllTypeface()
		{
			foreach (FontFamily fontFamily in FirePreventionThumbnailManager.GetAllTypeface())
			{
				this.cmbTypeface.Items.Add(fontFamily.Name);
			}
			int num = 0;
			if (this.cmbTypeface.Items.Count > 0)
			{
				if (this.cmbTypeface.Items.Contains(this.m_FirePreventionThumbnailSetUpDataSave.TypefaceName))
				{
					this.cmbTypeface.Text = this.m_FirePreventionThumbnailSetUpDataSave.TypefaceName;
					return;
				}
				IEnumerator enumerator2 = this.cmbTypeface.Items.GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Equals("仿宋"))
						{
							this.cmbTypeface.SelectedIndex = num;
							return;
						}
						num++;
					}
				}
				this.cmbTypeface.SelectedIndex = 0;
			}
		}

		private void FormFirePreventionThumbnailSetUp_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void txtCharacterHeight_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.SetData(e);
		}

		private void txtRatioOfHeightToWidth_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.SetData(e);
		}

		private void SetData(KeyPressEventArgs e)
		{
			if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '\b')
			{
				e.Handled = false;
				return;
			}
			e.Handled = true;
		}

		private void FormFirePreventionThumbnailSetUp_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void txtCharacterHeight_MouseLeave(object sender, EventArgs e)
		{
			this.JudgeFirePreventionThumbnailSetUpData(this.txtCharacterHeight, 3.5);
		}

		private void txtRatioOfHeightToWidth_MouseLeave(object sender, EventArgs e)
		{
			this.JudgeFirePreventionThumbnailSetUpData(this.txtRatioOfHeightToWidth, 0.7);
		}

		private FirePreventionThumbnailSetUpData m_FirePreventionThumbnailSetUpData = new FirePreventionThumbnailSetUpData();

		private FirePreventionThumbnailSetUpData m_FirePreventionThumbnailSetUpDataSave = new FirePreventionThumbnailSetUpData();

		private string m_strFilePath = Product.UserDataLocation + "\\FirePreventionThumbnailFormDataSetup.xml";
	}
}
